package com.irdstudio.efp.flow.api.rest;

import java.util.List;
import java.util.Map;

import com.irdstudio.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.efp.flow.service.facade.BizFlowNodeService;
import com.irdstudio.efp.flow.service.vo.BizFlowNodeVO;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.core.log.LogManager;
import com.irdstudio.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class BizFlowNodeController extends AbstractController  {

	@Autowired
	@Qualifier("bizFlowNodeService")
	private BizFlowNodeService bizFlowNodeService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/biz/flow/nodes", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<BizFlowNodeVO>> queryBizFlowNodeAll(
			@RequestBody BizFlowNodeVO vo) {		

		 LogManager.info(vo.getBizNodeId(), bizFlowNodeService, "queryAll");
		 
		//List<BizFlowNodeVO> outputVo = this.executeQueryList("queryAll", bizFlowNodeService, new Object[]{vo});
		
		List<BizFlowNodeVO> outputVo = null;
		
		// 根据用户角色获取用户权限方案
//		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		//DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType("01");
		switch (dataRuleType) {
		case Owner:
			outputVo = bizFlowNodeService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = bizFlowNodeService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = bizFlowNodeService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = bizFlowNodeService.queryAllOwner(vo);
			break;
		}
		
		return getResponseData(outputVo);

	}

	/**
	 * 根据业务流转配置编号查询节点信息
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/biz/flow/nodesByFlow", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<BizFlowNodeVO>> queryBizFlowNodeByFlow (
			@RequestBody BizFlowNodeVO vo) {		
		List<BizFlowNodeVO> outputVo = bizFlowNodeService.queryAllByFlowIdByPage(vo);
		return getResponseData(outputVo);
	}

	/**
	 * 根据业务流转配置ID和事件ID查询节点信息
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/biz/flow/nodesByEventAndFlow", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<BizFlowNodeVO>> queryBizFlowNodeByFlowAndEvent (
			@RequestBody BizFlowNodeVO vo) {		
		List<BizFlowNodeVO> outputVo = bizFlowNodeService.queryAllByFlowIdAndEventIdByPage(vo);
		return getResponseData(outputVo);
	}

	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/biz/flow/node/{bizNodeId}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<BizFlowNodeVO> queryByPk(@PathVariable("bizNodeId") String bizNodeId) {		
		BizFlowNodeVO inVo = new BizFlowNodeVO();
		inVo.setBizNodeId(bizNodeId);
		BizFlowNodeVO outputVo = bizFlowNodeService.queryByPk(inVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键删除信息
	 * @param bizFlowNode
	 * @return
	 */
	@RequestMapping(value="/biz/flow/node", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody BizFlowNodeVO inBizFlowNodeVo) {		
		int outputVo = bizFlowNodeService.deleteByPk(inBizFlowNodeVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 * @param inBizFlowNodeVo
	 * @return
	 */
	@RequestMapping(value="/biz/flow/node", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody BizFlowNodeVO inBizFlowNodeVo) {		
		int outputVo = bizFlowNodeService.updateByPk(inBizFlowNodeVo);
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 * @param inBizFlowNodeVo
	 * @return
	 */
	@RequestMapping(value="/biz/flow/node", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertBizFlowNode(@RequestBody BizFlowNodeVO inBizFlowNodeVo) {
		int outputVo = bizFlowNodeService.insertBizFlowNode(inBizFlowNodeVo);
		return getResponseData(outputVo);

	}
}
